package com.xcc.mota.screen;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.xcc.mota.R;
import com.xcc.mota.sprite.BaseSprite;
import com.xcc.mota.sprite.TextSprite;

import static com.xcc.mota.screen.BaseScreen.ScreenEvent.MsgMenu;
import static com.xcc.mota.screen.BaseScreen.ScreenEvent.TypeOpenScreen;

/**
 * Created by 磁磁帅 on 2018/1/10.
 * 关于
 */
public class AboutScreen extends BaseScreen implements BaseSprite.OnClickListener {
    public AboutScreen(Context context) {
        super(context);

        paint = new Paint();
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);

        initSprite();
    }

    private void initSprite() {
        textBack = new TextSprite().setTextColor(0xFFffffff).setText("返回").setTextSize(60);
        textBack.setX(90);
        textBack.setY(60);
        textBack.setOnClickListener(this);
        addTouchSprite(textBack);

        text1 = new TextSprite().setText("开发者：rookeCi").setTextSize(60);
        text1.setX(ScreenWidth / 2);
        text1.setY(600);

        text2 = new TextSprite().setText("代码已在码云开源").setTextSize(60);
        text2.setX(ScreenWidth / 2);
        text2.setY(700);
    }

    private int time;
    private Paint paint;
    private TextSprite textBack, text1, text2;
    private int b = 0x00, g = 0x00, r = 0xFF;
    private static final int addInt = 2;
    private int addType = 0;
    private Bitmap bitmap;

    public void onDraw(Canvas canvas) {

        if (addType == 0) {
            b += addInt;
            if (b > 0xFF) {
                b = 0xFF;
                addType = 1;
            }
        } else if (addType == 1) {
            g += addInt;
            if (g > 0xFF) {
                g = 0xFF;
                addType = 2;
            }
        } else if (addType == 2) {
            b -= addInt;
            if (b < 0x00) {
                b = 0x00;
                addType = 3;
            }
        } else if (addType == 3) {
            g -= addInt;
            if (g < 0x00) {
                g = 0x00;
                addType = 0;
            }
        }
        canvas.drawRGB(0, g, b);
        if (bitmap != null)
            canvas.drawBitmap(bitmap, (ScreenWidth - bitmap.getWidth()) / 2, 300, null);
        int argb = Color.argb(0xFF, r, g, b);
        text1.setTextColor(argb);
        text2.setTextColor(argb);

        textBack.onDraw(canvas, paint);
        text1.onDraw(canvas, paint);
        text2.onDraw(canvas, paint);

        if (++time < 0) time = 0;
    }

    public void onClick(BaseSprite sprite) {
        if (textBack == sprite) {
            if (onScreenEvent != null) {
                onScreenEvent.onEvent(TypeOpenScreen, MsgMenu);
            }
        }
    }
}
